package com.bruce.phoneFlash;

import android.app.Service;
import android.content.Intent;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.IBinder;

import com.bruce.timerTask.MyTimerTask;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    private final int DEFENT = 1;
    private final int SOS = 2;
    private final int IOS = 3;
    private Timer timer;
    private MyTimerTask task;
    private TimerTask closeTask;
    private TimerTask closeTask1;
    private TimerTask openTask;
    private TimerTask openTask1;
    private String cameraId;

    private MyApplication app;

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = (MyApplication) getApplication();
        int mode = app.getMode();
        if (app.getIsFlash() && app.getIsLow()) {
            timer = new Timer();
            task = MyTimerTask.getTask();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
                try {
                    String[] cameraIdList = cameraManager.getCameraIdList();
                    for (String s : cameraIdList) {
                        CameraCharacteristics camera = cameraManager.getCameraCharacteristics(s);
                        Boolean aBoolean = camera.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                        if (aBoolean != null) {
                            cameraId = s;
                            break;
                        }
                    }

                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
                task.setCameraManager(cameraManager, cameraId);
            } else {
                task.setCamera();
            }
            openTask = task.getOpenTask();
            closeTask = task.getCloseTask();
            flash(mode);
        }
    }

    @Override
    public void onDestroy() {
        app = null;
        if (timer != null) {
            timer.cancel();
        }
        if (openTask != null) {
            openTask.cancel();
        }
        if (openTask1 != null) {
            openTask1.cancel();
        }
        if (closeTask != null) {
            closeTask.cancel();
        }
        if (closeTask1 != null) {
            closeTask1.cancel();
        }
        if (task != null) {
            try {
                task.free();
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        super.onDestroy();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void flash(int mode) {

        switch (mode) {
            case DEFENT:
                timer.schedule(openTask, 0, 1000);
                timer.schedule(closeTask, 500, 1000);
                break;
            case SOS:
                timer.schedule(openTask, 0, 700);
                timer.schedule(closeTask, 500, 700);
                break;

            case IOS:
                timer.schedule(openTask, 0, 3400);
                timer.schedule(closeTask, 400, 3400);
                closeTask1 = task.getCloseTask();
                openTask1 = task.getOpenTask();
                timer.schedule(openTask1, 1400, 3400);
                timer.schedule(closeTask1, 2400, 3400);
                break;
        }
    }

}
